<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input placeholder="请输入内容" v-model="searchtext" clearable>
            <el-button
              slot="append"
              icon="el-icon-search"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="6"
          ><el-button type="primary" @click="changeVisible"
            >添加用户</el-button
          ></el-col
        >
      </el-row>
      <el-table :data="tableList" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="eamil" label="邮箱"> </el-table-column>
        <el-table-column prop="phone" label="电话"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="usersStausChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>

            <el-tooltip content="查看角色" placement="top" :enterable="false">
              <el-button
                type="warning"
                icon="el-icon-s-tools"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[1, 2, 6, 11]"
        :page-size="2"
        layout="total, sizes, prev, pager, next, jumper"
        :total="10"
      >
      </el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" @close="addrests">
      <el-form
        ref="addform"
        :rules="addrules"
        :model="addform"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="addform.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addform.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="eamil">
          <el-input v-model="addform.eamil"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addform.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addusers"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    // 自定义手机号规则
    var checkPhone = (rule, value, callback) => {
      let myreg = /^[1][3,4,5,7,8][0-9]{9}$/
      if (myreg.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的手机号'))
    }
    // 自定义邮箱规则
    var checkEamil = (rule, value, callback) => {
      const regEamil = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
      if (regEamil.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的邮箱格式'))
    }

    return {
      addform: {
        name: '',
        password: '',
        eamil: '',
        phone: '',
      },
      addrules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        eamil: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            validator: checkEamil,
            trigger: 'blur',
          },
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkPhone,
            trigger: 'blur',
          },
        ],
      },
      searchtext: '',
      currentPage4: 1,
      dialogVisible: false,
      tableList: [
        {
          name: '张三',
          eamil: '1017@qq.com',
          phone: 182335362,
          status: false,
        },
        {
          name: '李四',
          eamil: '1017@qq.com',
          phone: 182335362,
          status: true,
        },
        {
          name: '王五',
          eamil: '1017@qq.com',
          phone: 182335362,
          status: true,
        },
        {
          name: '赵六',
          eamil: '1017@qq.com',
          phone: 182335362,
          status: false,
        },
      ],
    }
  },
  created() {},
  methods: {
    handleSizeChange(newSize) {
      console.log('选择每页显示', newSize)
    },
    handleCurrentChange(val) {
      console.log('当前页', newSize)
    },
    usersStausChange(val) {
      console.log('开关状态', val.status)
    },
    changeVisible() {
      this.dialogVisible = true
      this.addrests
    },
    // 表单重置
    addrests() {
      this.$refs.addform.resetFields()
    },
    // 提交表单前校验
    addusers() {
      this.$refs.addform.validate((valid) => {
        console.log(valid)
      })
    },
  },
}
</script>
<style lang="less" scoped>
</style>